package com.example.coursaccino;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class RegisterActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_register);
    }
    
    
    private boolean is_string_null (String str, String field) {
    	System.out.println("Input string " + str);
    	if (str == null || str.equals("")) {
    		Toast msg = Toast.makeText(getBaseContext(), 
    				field + "cannot be empty", Toast.LENGTH_LONG);
    		msg.show();
    		System.out.println("Return true " + str);
    		
    		return true;
    	}
    	System.out.println("Return false " + str);
    	return false;
    }
    
    public void create (View view) {
    
    	String message;
    	
    	EditText username_et = (EditText) findViewById(R.id.username_et);
    	String username = username_et.getText().toString();
    	
    	if (is_string_null(username, "Username"))
    		return;
    	
    	EditText password_et = (EditText) findViewById(R.id.password_et);    	
    	String password = password_et.getText().toString();
    	
    	if (is_string_null(password, "Password"))
    		return;
    	
    	EditText designation_et = (EditText) findViewById(R.id.designation_et);
    	String desg = designation_et.getText().toString();
    	
    	if (is_string_null(desg, "Designation"))
    		return;
    	
    	User usr = new User (username, password, desg);
    	
    	if (WelcomeActivity.dbHelper.getModel().getUserDAO().
    				query_and_insert_user(usr)) {
    		// Print something on screen
    		message = "New user account successfully created. Please login";
        	Toast msg = Toast.makeText(getBaseContext(),message, Toast.LENGTH_LONG);
        	msg.show();
    
    		Intent intent = new Intent(this, WelcomeActivity.class);    	
        	startActivity(intent);
    	} else {
    		message = "A user already exists";
        	Toast msg = Toast.makeText(getBaseContext(),message, Toast.LENGTH_LONG);
        	msg.show();
    	}
    }
}
